﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace StudentManage.Utils
{
    /// <summary>
    /// 弹出窗口管理器封装
    /// </summary>
    public class WindowManager
    {
        /// <summary>
        /// 用户存放需要弹出的其它窗口
        /// </summary>
        private static Hashtable registerPages = new Hashtable();
        /// <summary>
        /// 注册 通过设置Key注册  泛型方法
        /// </summary>
        /// <typeparam name="T">要弹出窗体的对象</typeparam>
        /// <param name="key"></param>
        public static void Register<T>(string key)
        {
            if (!registerPages.ContainsKey(key)) // 判断子窗口是否注册过
            {
                registerPages.Add(key, typeof(T)); // 添加子窗口
            }
        }

        /// <summary>
        /// 通过key和Type对象注册
        /// </summary>
        /// <param name="key"></param>
        /// <param name="t">要弹出窗体的对象</param>
        public static void Register(string key, Type t)
        {
            if (!registerPages.ContainsKey(key)) // 判断子窗口是否注册过
            {
                registerPages.Add(key, t); // 添加子窗口
            }
        }

        /// <summary>
        /// 根据Key移除指定的窗体
        /// </summary>
        /// <param name="key"></param>
        public static void Remove(string key)
        {
            if (registerPages.ContainsKey(key)) // 判断子窗口是否注册过
            {
                registerPages.Remove(key); // 移除指定的窗口
            }
        }

        /// <summary>
        /// 打开窗口页面  Show
        /// </summary>
        /// <param name="key">要打开的子窗体Key</param>
        /// <param name="vm">DataContext</param>
        public static void Show(string key, object vm)
        {
            if (!registerPages.ContainsKey(key)) // 判断子窗口是否注册过
            {
                throw new Exception("没有注册此键！");
            }
            // 获取窗体
            Type type = (Type)registerPages[key];
            // 创建实例 - Window对象-用户自定义控件也可以转成Window
            var win = (Window)Activator.CreateInstance(type);
            // 绑定
            win.DataContext = vm;
            // 设置弹窗显示位置
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            // 打开窗口
            win.Show();
        }

        /// <summary>
        /// 打开窗口页面  ShowDialog
        /// </summary>
        /// <param name="key">要打开的子窗体Key</param>
        /// <param name="vm">DataContext</param>
        /// <exception cref="Exception"></exception>
        public static void ShowDialog(string key, object vm)
        {
            if (!registerPages.ContainsKey(key))
            {
                throw new Exception("没有注册此键！");
            }
            //Window对象-用户自定义控件也可以转成Window
            var win = (Window)Activator.CreateInstance((Type)registerPages[key]);
            // 绑定
            win.DataContext = vm;
            // 用于解决在某中情况下子窗口覆盖在弹出窗口上问题
            //win.Owner = ((Window)registerPages[key]).Owner;
            // 设置弹窗显示位置
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            // 打开窗口
            win.ShowDialog();
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="win"></param>
        public static void Close(object win)
        {
            if (win != null)
            {
                Window window = win as Window;
                window.Close(); // 关闭窗体
            }
        }

        /// <summary>
        /// 隐藏窗体
        /// </summary>
        /// <param name="win"></param>
        public static void Hide(object win)
        {
            if (win != null)
            {
                Window window = win as Window;
                window.Hide(); // 隐藏窗体
            }
        }
    }
}
